using System;
using System.Collections.Generic;
using System.Text;
using Csla.Data;
using Csla;
using Active.Core.DB;

namespace Active.Core
{
    [Serializable()]
    public class Product : BusinessBase<Product>
    {
        #region Business Methods

        // TODO: add your own fields, properties and methods
        private int _id;

        public int Id
        {
            get
            {
                CanReadProperty(true);
                return _id;
            }
        }

        string _name = string.Empty;
        public string Name
        {
            get
            {
                CanReadProperty(true);
                return _name;
            }
            set
            {
                CanWriteProperty(true);
                if (_name != value)
                {
                    _name = value;
                    PropertyHasChanged();
                }
            }
        }

        string _versions = string.Empty;
        public string Versions
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _versions ?? string.Empty;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_versions != value)
                {
                    _versions = value;
                    PropertyHasChanged();
                }
            }
        }

        int _customerId;
        public int CustomerId
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _customerId;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_customerId.Equals(value))
                {
                    _customerId = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static Product NewProduct()
        {
            // TODO: change to use new keyword if not loading defaults
            return new Product();
            //return DataPortal.Create<Product>();
        }

        internal static Product GetProduct(SafeDataReader dr)
        {
            return new Product(dr);
        }

        private Product()
        {
            MarkAsChild();
        }

        private Product(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            _id = dr.GetInt32("Id");
            _customerId = dr.GetInt32("CustomerId");
            _name = dr.GetString("Name");
            _versions = dr.GetString("Versions");

            MarkOld();
        }

        internal void Insert(Customer parent)
        {
            _customerId = parent.Id;

            NBearLite.InsertSqlSection insert =
                DataService.DBService.Insert(Tables.Products)
                .AddColumn(Tables.Products.Name, _name)
                .AddColumn(Tables.Products.Versions, _versions)
                .AddColumn(Tables.Products.CustomerId, _customerId);
            _id = insert.ExecuteReturnAutoIncrementID(Tables.Products.Id);
            MarkOld();
        }

        internal void Update(Customer parent)
        {
            _customerId = parent.Id;
            NBearLite.UpdateSqlSection update =
                DataService.DBService.Update(Tables.Products)
                .AddColumn(Tables.Products.Name, _name)
                .AddColumn(Tables.Products.Versions, _versions)
                .AddColumn(Tables.Products.CustomerId, _customerId)
                .Where(Tables.Products.Id == _id);
            update.Execute();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            DataService.DBService.Delete(Tables.Products)
                .Where(Tables.Products.Id == _id).Execute();
            MarkNew();
        }

        #endregion
    }
}
